package customers.restclient;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import customers.model.MarketCoupon;
import customers.model.MarketCouponReq;
import customers.model.MarketCouponResp;
import customers.service.MarketCouponService;

@Controller
@RequestMapping(value="/marketCouponInfoRest",method={RequestMethod.POST})
public class MarketCouponInfoRest {
	
	private final static Logger logger = LoggerFactory.getLogger(MarketCouponInfoRest.class);
	
	@Autowired
	private MarketCouponService marketCouponService;
    /**
     * 添加
     * @param parm
     * @return
     */
	@RequestMapping("/addCoupon")
	@ResponseBody
	public MarketCouponResp<MarketCoupon> insertMarketCoupon(@RequestBody MarketCoupon parm){
		logger.info("添加优惠券："+JSONObject.toJSONString(parm));
		MarketCoupon marketCoupon = new MarketCoupon();
	    marketCoupon.setAmount(parm.getAmount());
        marketCoupon.setBusinessId(parm.getBusinessId());
        marketCoupon.setCategory(parm.getCategory());
        marketCoupon.setCouponStatus(parm.getCouponStatus());
        marketCoupon.setCustomerId(parm.getCustomerId());
        
		marketCoupon.setEndTime(parm.getEndTime());
		marketCoupon.setStartTime(parm.getStartTime());
		marketCoupon.setFreezeTime(parm.getFreezeTime());
		marketCoupon.setLimitAmountMin(parm.getLimitAmountMin());
		marketCoupon.setLimitDurationMax(parm.getLimitDurationMax());
		marketCoupon.setLimitDurationMin(parm.getLimitDurationMin());
		
		marketCoupon.setMobile(parm.getMobile());
		marketCoupon.setRuleDesc(parm.getRuleDesc());
		marketCoupon.setRuleName(parm.getRuleName());
		marketCoupon.setSendRuleType(parm.getSendRuleType());
		
		marketCoupon.setSource(parm.getSource());
		marketCoupon.setSourceDesc(parm.getSourceDesc());
		marketCoupon.setVerificationTime(parm.getVerificationTime());
		marketCoupon.setSendType(parm.getSendType());
		MarketCouponResp<MarketCoupon> resp = marketCouponService.insertMarketCoupon(marketCoupon);
		return resp;	
	}
	/**
	 * 查询按会员ID
	 * @param param
	 * @return
	 */
	@RequestMapping("/queryAllCoupons")
	@ResponseBody
	public MarketCouponResp<List<MarketCoupon>> queryAllCouponsByCustomerId(@RequestBody Map param){
	    int customerId = Integer.parseInt((String)param.get("customerId"));
	    MarketCouponReq<MarketCoupon> req = new MarketCouponReq<>();
	    MarketCoupon marketCoupon = new MarketCoupon();
	    marketCoupon.setCustomerId(customerId);
	    req.setBody(marketCoupon);
		MarketCouponResp<List<MarketCoupon>> resp = marketCouponService.selectCouponByCustomerId(req);
		return resp;
	}
	/**
	 * 冻结
	 * @param parm
	 * @return
	 */
	@RequestMapping("/freezeCoupon")
	@ResponseBody
	public MarketCouponResp<MarketCoupon> freezeCoupon(@RequestBody Map parm){
		String businessId = (String) parm.get("businessId");
		MarketCouponReq<MarketCoupon> req = new MarketCouponReq<>();
	    MarketCoupon marketCoupon = new MarketCoupon();
	    marketCoupon.setBusinessId(businessId);
	    req.setBody(marketCoupon);
	    MarketCouponResp<MarketCoupon> resp = marketCouponService.frezzeCoupon(req);
	    return resp;
	}
	
	@RequestMapping("/unfreezeCoupon")
	@ResponseBody
	public MarketCouponResp<MarketCoupon> unfreezeCoupon(@RequestBody Map parm){
		String businessId = (String) parm.get("businessId");
		MarketCouponReq<MarketCoupon> req = new MarketCouponReq<>();
	    MarketCoupon marketCoupon = new MarketCoupon();
	    marketCoupon.setBusinessId(businessId);
	    req.setBody(marketCoupon);
	    MarketCouponResp<MarketCoupon> resp = marketCouponService.unfrezzeCoupon(req);
	    return resp;
	}
	
	@RequestMapping("/verificationCoupon")
	@ResponseBody
	public MarketCouponResp<MarketCoupon> verificationCoupon(@RequestBody Map parm){
		String businessId = (String) parm.get("businessId");
		MarketCouponReq<MarketCoupon> req = new MarketCouponReq<>();
	    MarketCoupon marketCoupon = new MarketCoupon();
	    marketCoupon.setBusinessId(businessId);
	    req.setBody(marketCoupon);
	    MarketCouponResp<MarketCoupon> resp = marketCouponService.verificationCoupon(req);
	    return resp;
	}
	
}
